package ex110;

import java.io.InputStream;
import java.util.Scanner;

public class ScoreProcessor {
    private double[][] scoreList;     // 存放成绩的二维数组
    private double[][] resultList;    // 存放统计结果的二维数组

    /**
     * 读取输入的成绩
     *
     * @param in 输入来源
     */
    public void load(InputStream in) {
        Scanner scanner = new Scanner(in);
        int numberOfClass = scanner.nextInt(); // 输入班级数

        scoreList = new double[numberOfClass][]; // 创建行数组

        for (int row = 0; row < scoreList.length; row++) {
            scoreList[row] = new double[scanner.nextInt()]; // 创建第row行的列数组
            for (int col = 0; col < scoreList[row].length; col++) {
                scoreList[row][col] = scanner.nextDouble();
            }
        }

        scanner.close();
    }

    /**
     * 处理成绩，结果存放到 resultList
     */
    public void process() {
        resultList = new double[scoreList.length][3];
        for (int i = 0; i < scoreList.length; i++) {
            resultList[i][0] = average(scoreList[i]);
            resultList[i][1] = getHighest(scoreList[i]);
            resultList[i][2] = getLowest(scoreList[i]);
        }
    }

    /**
     * @return 处理后的结果
     */
    public double[][] getResultList() {
        return resultList;
    }

    private double average(double[] list) {
        double sum = 0;
        for (double score : list) {
            sum += score;
        }
        return sum / list.length;
    }

    private double getHighest(double[] list) {
        double result = list[0];
        for (int i = 1; i < list.length; i++) {
            if (list[i] > result) {
                result = list[i];
            }
        }
        return result;
    }

    private double getLowest(double[] list) {
        double result = list[0];
        for (int i = 1; i < list.length; i++) {
            if (list[i] < result) {
                result = list[i];
            }
        }
        return result;
    }

}
